package com.teragadgets.android.comics.appengine;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.datanucleus.util.StringUtils;

@SuppressWarnings("serial")
public class ComicRequestServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(AddComicServlet.class
			.getName());

	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		int webComicID = Integer.parseInt(req.getParameter("id"));
		resp.getWriter().println("<?xml version=\"1.0\" encoding='UTF-8'?>");
		resp.getWriter().println("<webcomic>");
		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			String query = "select from " + Comic.class.getName()
					+ " where webComic == webComicParam "
					+ "parameters int webComicParam "
					+ "order by number desc";
			List<Comic> webComics = (List<Comic>) pm.newQuery(query).execute(
					webComicID);
			for (Comic c : webComics)
				resp.getWriter().println(
						"<comic name='" + c.getName() + "' number='"
								+ c.getNumber() + "' url='" + c.getURL()
								+ "' alt='${fn:escapeXml(" + c.getAltText() + ")' />");
		} finally {
			pm.close();
		}

		resp.getWriter().println("</webcomic>");
	}
}